home *** CD-ROM | disk | FTP | other *** search
- Path: netnews.whoi.edu!NewsWatcher!user
- From: davef@mbl.edu (Dave Fernandes)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] equivalent or better strtok() in C++?
- Date: Fri, 26 Jan 1996 12:05:17 -0500
- Organization: Marine Biological Laboratory
- Message-ID: <davef-2601961205170001@128.128.173.90>
- References: <4e6p1m$anc@walrus2.walrus.com>
- NNTP-Posting-Host: 128.128.173.90
-
- In article <4e6p1m$anc@walrus2.walrus.com>, fjordao@walrus.com (Felipe
- Jordao) wrote:
-
- > Hi,
- >
- > I have been trying to use strtok() to get tokens from a delimited
- > string but it only recognizes tokens with one or more characters. (I
- > know this is how the function is defined).
- >
- > However...
-
-
- I don't know of any pretty way to do this, but I use the following
- function. It uses the ANSI <string> class.
-
- Arguments:
- line - the input string
- delimit - the char* used to delimit the text (Ex: char *delimit = ".")
- array - the output array of strings
- If you want to get a char* from this, use:
- char *arrayElement_i = array[i].c_str();
- n_max - the size of array[]. This is the maximum number of items that will
- be scanned in.
-
- The function returns the number of items that were actually scanned in.
-
- Hope this helps.
- Dave
-
- //____________________________________________________________________________
-
- #include <string>
-
- int StringScan(const string &line, const char *delimit, string array[],
- int n_max)
- {
- const string delimiter(delimit);
- size_t len = line.length();
- size_t start = 0;
- int nFound = 0;
-
- while (nFound < n_max) {
- size_t end = line.find_first_of(delimiter, start);
- if (end > len) end = len;
-
- if (end < start)
- break;
- else if (end > start)
- array[nFound++] = line.substr(start, end-start);
- else
- array[nFound++] = "";
- start = end + 1;
- }
-
- return nFound;
- }
-
- --
- Dave Fernandes
- Postdoctoral Research Associate
- The Ecosystems Center
- Marine Biological Laboratory
-